package com.example.ttl1.message;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * ClassName:ReceiveMessage
 * Package:com.example.ttl1.message
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/8/16-17:48
 * @Version:v1.0
 */
@Component
@Slf4j
public class ReceiveMessage {

    @RabbitListener(queues = "queue.reliability.1")
    public void receiveMsg(Message message,Channel channel){

        //获取消息的唯一标识
        long deliveryTag = message.getMessageProperties().getDeliveryTag();


        try {
            log.info("接收到的消息为:{}",new String(message.getBody()));
            //TODO 插入订单
//            int a=1/0;
            //手动确认
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            log.error("消息处理问题");
            try {
                channel.basicNack(deliveryTag,false,true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }
    }
}
